<?php

namespace App\Http\Controllers;

use App\Models\Customer;
use Exception;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    /**
     * @throws Exception
     */
    public function __construct(Request $request)
    {
        $token = $request->header('token');
        if (!empty($token)) {
            $customer = Customer::where('token', $token)->first();
            if (empty($customer)) {
                throw new Exception('验证失败');
            }
            $request->offsetSet('customer_id', $customer->id);
        }
    }
}
